from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score


def lineRegres(x_train, y_train, x_test, y_test):
    # 1.使用默认配置初始化学习器实例
    lr = LinearRegression()

    # 2.用训练数据训练模型参数
    lr.fit(x_train, y_train)

    # 3. 用训练好的模型对测试集进行预测
    y_test_pred_lr = lr.predict(x_test)
    y_train_pred_lr = lr.predict(x_train)

    # 使用r2_score评价模型在测试集和训练集上的性能，并输出评估结果
    # 测试集
    print('The r2 score of LinearRegression on test is', r2_score(y_test, y_test_pred_lr))
    # 训练集
    print('The r2 score of LinearRegression on train is', r2_score(y_train, y_train_pred_lr))
    return lr.coef_, y_train_pred_lr
